import {
  INCLUDE_TAG,
  EXCLUDE_TAG,
  END_TAG,
  SUBPACKAGE_PAGE,
  HOME_PAGE,
  TAB_PAGE,
  OPT_KEYS,
  PRIMARY_KEYS,
  hasOwn,
  clone
} from '@shared';
import {
  formatPlatforms,
  findTabBarIndex,
  formatTabBar,
  joinSubpackagePath
} from '@core/util';

// profile
function getProfile(pagesJson) {
  let options = {};
  let extras = {};
  const keys = Object.keys(pagesJson);
  keys.forEach(key => {
    if (~OPT_KEYS.indexOf(key)) {
      options[key] = pagesJson[key];
    } else if (key === 'tabBar') {
      options[key] = formatTabBar(pagesJson[key]);
    } else if (!~PRIMARY_KEYS.indexOf(key)) {
      extras[key] = pagesJson[key];
    }
  });

  return {
    ...options,
    extras,
    theme: {
      '$primaryColor': ''
    }
  };
}
// routers
function getRouters(data, options) {
  let pages = [];
  let root = '';
  let includeValue, excludeValue;
  if (hasOwn(data, 'root')) {
    root = data.root;
    pages = data.pages;
    if (options) {
      if (hasOwn(options, INCLUDE_TAG)) {
        includeValue = options[INCLUDE_TAG];
      } else if (hasOwn(options, EXCLUDE_TAG)) {
        excludeValue = options[EXCLUDE_TAG];
      }
    }
  } else {
    pages = data;
    //TODO 首页也有可能出现条件编译，这种情况暂不考虑。
    pages[0][HOME_PAGE] = true;
  }
  // main
  const routers = [];
  // 遍历
  let element, i = 0,
    amount = pages.length;
  for (; i < amount; i++) {
    element = pages[i];
    // 条件编译
    if (hasOwn(element, INCLUDE_TAG)) {
      includeValue = formatPlatforms(element[INCLUDE_TAG]);
      continue;
    } else if (hasOwn(element, EXCLUDE_TAG)) {
      excludeValue = formatPlatforms(element[EXCLUDE_TAG]);
      continue;
    } else if (hasOwn(element, END_TAG)) {
      includeValue = excludeValue = null;
      continue;
    }
    // 这里一定要合并生成新数组，否则后面值变化会影响最终结果。
    if (includeValue) {
      element[INCLUDE_TAG] = [].concat(includeValue);
    } else if (excludeValue) {
      element[EXCLUDE_TAG] = [].concat(excludeValue);
    }
    // tabBar
    if (options && hasOwn(options, 'list')) {
      const tabBarIndex = findTabBarIndex(element.path, options.list);
      if (~tabBarIndex) {
        const tabItem = clone(options.list[tabBarIndex]);
        // 移除 pagePath 属性，防止造成开发期间的干扰。
        delete tabItem.pagePath;
        element[TAB_PAGE] = tabItem;
      }
    }
    // subpackages
    if (root) {
      element.path = joinSubpackagePath(root, element.path);
      element[SUBPACKAGE_PAGE] = {
        root
      };
    }
    // add
    routers.push(element);
  }
  return routers;
}
// subpackages
function getSubpackages(subpackages) {
  let routers = [];
  // 会出现某个分包整体包裹在一个条件编译下的情况
  let includeValue, excludeValue,
    element, subpackagePages = [],
    i = 0,
    amount = subpackages.length;
  for (; i < amount; i++) {
    element = subpackages[i];
    // 条件编译
    if (hasOwn(element, INCLUDE_TAG)) {
      includeValue = element;
      continue;
    } else if (hasOwn(element, EXCLUDE_TAG)) {
      excludeValue = element;
      continue;
    } else if (hasOwn(element, END_TAG)) {
      includeValue = excludeValue = null;
      continue;
    }
    // 将 subpackages 页面规范转换为标准的 pages 页面规范
    subpackagePages = getRouters(element, includeValue || excludeValue);
    // concat
    routers = routers.concat(subpackagePages);
  }
  return routers;
}
// main
export default function parsePagesJson(pagesJsonString) {
  const pagesJson = JSON.parse(pagesJsonString);
  // pages & tabBar
  const pages = getRouters(pagesJson.pages, pagesJson.tabBar);
  // subpackages
  const subpackages = pagesJson.subpackages ? getSubpackages(pagesJson.subpackages) : [];
  // 合并数据
  const routers = pages.concat(subpackages);
  // profile
  const profile = getProfile(pagesJson);

  return {
    routers,
    ...profile
  }
}
